﻿using SmartSpace.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SmartSpace.OA
{
    public partial class OAService : IOAService
    {
        public ServiceResult SaveAsset(Asset asset)
        {
            using (OADbContext context = new OADbContext())
            {
                EntityRepository.SaveCodeEntity(asset, context);
                return new ServiceResult(EntityRepository.Commit(context));
            }
        }

        public PagedList<Asset> GetAsset(string filter, Guid orgId, int start, int limit)
        {
            using (OADbContext context = new OADbContext())
            {
                List<Asset> list = context.AssetSet.Where(p => p.OrgId == orgId && p.Caption.Contains(filter))
                    .OrderBy(p => p.CreatedDate)
                    .Skip(start)
                    .Take(limit).ToList();
                return new PagedList<Asset>(list, start, limit, list.Count);
            }
        }

        public PagedList<Asset> GetAsset(string filter, string type, Guid orgId, int start, int limit)
        {
            using (OADbContext context = new OADbContext())
            {
                List<Asset> list = context.AssetSet.Where(p => p.TypeCode == type && p.OrgId == orgId && p.Caption.Contains(filter))
                    .OrderBy(p => p.CreatedDate)
                    .Skip(start)
                    .Take(limit).ToList();
                return new PagedList<Asset>(list, start, limit, list.Count);
            }
        }

        public Asset GetAsset(Guid id)
        {
            using (OADbContext context = new OADbContext())
            {
                return context.AssetSet.Where(p => p.Id == id).SingleOrDefault();
            }
        }

        public Asset GetAsset(string code)
        {
            using (OADbContext context = new OADbContext())
            {
                return context.AssetSet.Where(p => p.Code == code).SingleOrDefault();
            }
        }

        public ServiceResult DeleteAsset(Guid id)
        {
            using (OADbContext context = new OADbContext())
            {
                Asset asset = context.AssetSet.Where(p => p.Id == id).SingleOrDefault();
                EntityRepository.DeleteCodeEntity(asset, context);
                return new ServiceResult(EntityRepository.Commit(context));
            }
        }
    }
}
